<template>
	<ex-dialog v-model="visible" draggable :title="form.orgName ? form.orgName : (form.id ? form.orgName : '新增')"
  width="700" :contentHeight="450">
	  <template #default>
		<el-tabs v-model="activeName"  style="height: 100%">
		  <el-tab-pane label="基本信息" name="base" style="height: 100%">
			<el-row :gutter="80">
			  <el-col :lg="24">
				<el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="120px" label-position="left">
				  <el-form-item label="类型" prop="orgType">
					  <ex-radio-group v-model="form.orgType" dict-code="ORG_TYPE" button/>
				  </el-form-item>
					<el-form-item label="盘点报告汇总标识" prop="other.classificationTag">
						<ex-cascader v-model="form.other.classificationTag" dict-code="SysOrganization" key-prop="levelCode" placeholder="盘点报告汇总上级标识" ></ex-cascader>
<!--						<ex-radio-group v-model="form.other.classificationTag" dict-code="CLASSIFICATION_TAG" button/>-->
					</el-form-item>
<!--				  <el-form-item label="机构编码" prop="orgCode">-->
<!--					<el-input v-model="form.orgCode" clearable placeholder="机构编码"></el-input>-->
<!--				  </el-form-item>-->
				  <el-form-item label="机构名称" prop="orgName">
					<el-input v-model="form.orgName" clearable placeholder="机构名称"></el-input>
				  </el-form-item>
				  <el-form-item label="负责人" prop="leaderId">
					  <ex-selector :model="form" code="SysUser" prop="leaderId" :isicon="true"></ex-selector>
				  </el-form-item>
				  <el-form-item label="上级编码" prop="parentLevelCode">
            <ex-cascader v-model="form.parentLevelCode" dict-code="SysOrganization" key-prop="levelCode" placeholder="上级机构" ></ex-cascader>
<!--                  <el-cascader v-model="form.parentLevelCode" :options="deptOptions" :props="deptProps"-->
<!--                               :show-all-levels="false" placeholder="上级机构" clearable filterable></el-cascader>-->
           </el-form-item>
				<el-form-item label="机构简称" prop="simpleName">
					<el-input v-model="form.simpleName" clearable placeholder="机构简称"></el-input>
				  </el-form-item>
				  <el-form-item  label="承担成本中心" prop="holdCostCenter">
					<ex-selector code="FamsCostCenter" :model="form" prop="holdCostCenter" :isicon="true"></ex-selector>
				  </el-form-item>
				  <el-form-item label="制单成本中心" prop="createCostCenter">
						<ex-selector code="FamsCostCenter" :model="form" prop="createCostCenter" :isicon="true"></ex-selector>
				  </el-form-item>
				  <el-form-item label="共享中心" prop="isShareCenter">
					  <ex-select v-model="form.isShareCenter" placeholder="请选择是否已上线共享中心" dict-code="IS_SHARE"/>
				  </el-form-item>
				  <el-form-item label="排序" prop="orderWeight">
					<el-input v-model="form.orderWeight" clearable placeholder="排序"></el-input>
				  </el-form-item>
				  <el-form-item label="备注" prop="description">
					<el-input v-model="form.description" clearable placeholder="备注"></el-input>
				  </el-form-item>
				</el-form>

			  </el-col>
			</el-row>
		  </el-tab-pane>
		  <el-tab-pane label="可用位置" name="Sysposition" style="height: 100%">
			<ex-rel-table ref="positionRelTable" size="small" :rel-entity="form" :treeApi="this.$API.system.position.treeData" default-expand-all>

			</ex-rel-table>
		  </el-tab-pane>
		</el-tabs>
	  </template>
	  <template #footer>
		<div style="flex: auto;text-align: center">
		  <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
		  <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
		  <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
		</div>
	  </template>

	</ex-dialog>
  </template>

  <script>
  import scIconSelect from '@/components/scIconSelect'
  import ExRelTable from "@/components/exRelTable/index.vue";
  import ExSelector from "@/components/exSelector/index.vue";
  import ExRadioButtons from "@/components/exRadioGroup/index.vue";
  import ExRadioGroup from "@/components/exRadioGroup/index.vue";
  import ExSelect from "@/components/exSelect/index.vue";
  import ExCascader from "@/components/exCascader/index.vue";
  import exDialog from '@/components/exDialog';
  import BaseTemplate from "@/utils/baseTemplate";

  export default {
	emits: ['success'],
	components: {
    ExCascader,
		ExSelect,
		ExRadioGroup,
		ExRadioButtons,
		ExSelector,
	  ExRelTable,
	  scIconSelect,
	  exDialog
	},
	props: {
	  menu: {
		type: Object, default: () => {
		}
	  },
	},
	data() {
	  return {
		activeName: 'base',
		visible: false,
		form: {
		  id: "",
		  orgName: "",
		  orgCode: "",
		  orgType: "",
		  orgValue: "",
		  description: "",
		  parentLevelCode:"",
		},
		deptOptions: [],
		deptProps: {
		  value: 'levelCode',
		  label: 'label',
		  checkStrictly: true,
      expandTrigger:'hover',
		  emitPath:false,
		},
		button_rules: {
			orgType: [
			{required: true, message: "请选择组织机构类型", trigger: "change"},
		  ],
			'other.classificationTag': [
			{required: true, message: "请选择组织机构分类标识", trigger: "change"},
		  ],
		  orgName: [
			{required: true, message: "请输入组织机构名称", trigger: "change"},
		  ],
		  orgValue: [
			{required: true, message: "请输入组织机构值", trigger: "change"},
		  ],
		  parentLevelCode: [
			{required: true, message: "请选择上级", trigger: "change"},
		  ],
		// 	holdCostCenter:[
		// 		{required: true, message: "承担成本中心", trigger: "blur"},
		// 	],
      // createCostCenter:[
      //   {required: true, message: "制单成本中心", trigger: "blur"},
      // ],
		},
		loading: false,
		oldForm: null,
		  orgTypes:[],
	  }
	},
	watch: {},
	  computed:{
	},
	  mounted() {

	},
	onLoad() {
	},
	methods: {
		async initData() {
			this.deptOptions = await this.$cache.tree.options(this.$API.system.dept.treeData.storeName, {});
		},
	  relHandler(relMap) {
		if (!this.form.relMaps) {
		  this.form.relMaps = {};
		}
		for (let key in relMap) {//清空旧的数据
		  this.form.relMaps[key] = {};
		}
		//重新赋值新数据
		Object.assign(this.form.relMaps, relMap);
	  },
	  //显示
	  open(entity) {
			if(!entity.other){
				entity.other = {};
			}
		this.form = Object.assign({}, entity);
		this.oldForm = Object.assign({}, this.form);
		this.visible = true;
		this.activeName = "base";
		this.initData();
	  },
	  reset() {
		this.form = Object.assign({}, this.oldForm);
		  for(let ref in this.$refs){
			  try {
				  this.$refs[ref].refresh();
			  }catch (e){
			  }
		  }
	  },
	  //保存
	  async save() {
      let valid = await BaseTemplate.validate(this,this.$refs.dialogForm);

		if (valid) {

		  try {
			this.loading = true
			var res;
			if (this.form.id) {
			  res = await this.$API.system.dept.edit.post(this.form)
			} else {
			  res = await this.$API.system.dept.add.post(this.form)
			}
			if (res.code === 2000) {
			  this.$emit('success');
			  this.$message.success(this.$t("code."+res.code));
			  this.visible = false;
			  this.$store.dispatch("commit",this.form.entityName);
			} else {
			  this.$message.warning(res.message)
			}
		  } finally {
			this.loading = false
		  }
		}
	  },
	}
  }
  </script>

  <style scoped>
  h2 {
	font-size: 17px;
	color: #3c4a54;
	padding: 0 0 30px 0;
  }

  [data-theme="dark"] h2 {
	color: #fff;
  }

  </style>
